//
//  XLBaseEmptyTableVC.m
//  YangWifi
//
//  Created by zuoxueliangduyuanzhuo on 2017/6/15.
//  Copyright © 2017年 zuoxueliangduyuanzhuo. All rights reserved.
//

#import "XLBaseEmptyTableVC.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>
@interface XLBaseEmptyTableVC ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>


@end

@implementation XLBaseEmptyTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self.view addSubview:self.xlTableV];
    
    self.xlTableV.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 1)];
    self.xlTableV.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 1)];


    self.isShowEmptyData = YES;
    self.xlTableV.emptyDataSetSource = self;
    self.xlTableV.emptyDataSetDelegate = self;


}


- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    if (self.noDataImgName) {
        return [UIImage imageNamed:self.noDataImgName];
    }
    return [UIImage imageNamed:@""];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = self.noDataTitle?self.noDataTitle:@"亲，您暂时没有任何消息!";
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],
                                 NSForegroundColorAttributeName: [UIColor grayColor]};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = self.noDataDetailTitle;
    
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],
                                 NSForegroundColorAttributeName: [UIColor lightGrayColor],
                                 NSParagraphStyleAttributeName: paragraph};
    
    return self.noDataDetailTitle?[[NSAttributedString alloc] initWithString:text attributes:attributes]:nil;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
{
    return YES;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
{
    return self.isShowEmptyData;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return -self.xlTableV.tableHeaderView.frame.size.height/2.0f;
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],NSForegroundColorAttributeName:xlHexColor(@"165c4f")};
    
    return self.btnTitle?[[NSAttributedString alloc] initWithString:self.btnTitle attributes:attributes]:nil;
}

- (UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    NSString *imageName = self.btnBgImgName;
    NSString *title = self.btnTitle;
    
    if (state == UIControlStateNormal) imageName = self.btnBgImgName;
    if (state == UIControlStateHighlighted) imageName = self.btnBgSelectedImgName;
    
    UIEdgeInsets capInsets = UIEdgeInsetsMake(25, 25, 25, 25);
    
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attributes = @{NSFontAttributeName:xlFont(12), NSParagraphStyleAttributeName:paragraphStyle.copy};
    CGRect rect = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    
    CGFloat padding =  SCREEN_WIDTH/16.0;
    
    CGFloat leftPadding = -(SCREEN_WIDTH-2*padding-2*18-rect.size.width-10)/2.0;
    
    UIEdgeInsets rectInsets = UIEdgeInsetsMake(-19, leftPadding, -19, leftPadding);


    UIImage *image = [UIImage imageNamed:imageName inBundle:[NSBundle bundleForClass:[self class]] compatibleWithTraitCollection:nil];
    
    return [[image resizableImageWithCapInsets:capInsets resizingMode:UIImageResizingModeStretch] imageWithAlignmentRectInsets:rectInsets];

}

- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    return self.btnImgName?[UIImage imageNamed:self.btnImgName]:nil;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    [self buttonEvent:button.titleLabel.text];
}

#pragma mark 按钮事件
-(void)buttonEvent:(NSString *)title
{
    
}

- (NSMutableArray *)data{
    if (!_data) {
        _data = [NSMutableArray arrayWithCapacity:0];
    }
    return _data;
}

@end
